package com.fdb.efp.console.service;

import com.fdb.basic.framework.core.util.TimeUtil;
import com.fdb.basic.framework.core.vo.BaseInfo;
import com.fdb.basic.framework.core.vo.EngineHttpRequestVO;
import com.fdb.basic.framework.web.util.HttpClientUtil;
import com.fdb.efp.console.service.facade.SequenceService;
import com.fdb.efp.console.common.ConsoleConstant;
import com.fdb.efp.console.common.constance.FlowConstance;
import com.fdb.efp.console.service.facade.FlowUrlConfigService;
import com.fdb.efp.console.common.ExcelException;
import com.fdb.efp.console.service.facade.ColltTaskDistrAppService;
import com.fdb.efp.console.service.vo.ColltTaskDistrAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * 需求编号【2019D0509】
 *
 * @author qiuyf
 * @version 0.1 初始为0.1,后续变更则修改
 * @since 2019/09/20 15:26
 */
@Service("outsDistrCaseComnService")
public class OutsDistrCaseComnService {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(OutsDistrCaseComnService.class);

    @Autowired
    @Qualifier("colltTaskDistrAppService")
    private ColltTaskDistrAppService colltTaskDistrAppService;

    @Autowired
    @Qualifier("flowUrlConfigService")
    private FlowUrlConfigService flowUrlConfigService;

    @Autowired
    @Qualifier("dbSeqService")
    private SequenceService sequenceService;

    @Autowired
    private FlowCommonService flowCommonService;


    public void submitDistrCaseAppr(String distrCaseApply, String batchNo, String colltWay, BaseInfo vo) throws Exception {
        logger.info("发起外催任务分案申请流程开始，分案申请流水号为:" + distrCaseApply);
        String flowId = "FW20191011000000008";
        String eventId = FlowConstance.COLLT_TASK_DISTR_APP_ENTER;
        ColltTaskDistrAppVO ctdAppVO = new ColltTaskDistrAppVO();
        ctdAppVO.setDistrCaseApply(distrCaseApply);
        ctdAppVO.setColltBatchNo(batchNo);
        ctdAppVO.setColltWay(colltWay);
        ctdAppVO.setApproveStatus(ConsoleConstant.APRV_STATUS_02);
        ctdAppVO.setCreateUser(vo.getLoginUserId());
        ctdAppVO.setCreateTime(TimeUtil.getCurrentDateTime());
        ctdAppVO.setLastUpdateUser(vo.getLoginUserId());

        EngineHttpRequestVO eVo = new EngineHttpRequestVO();
        eVo.setBizLagelOrgCode(vo.getLoginUserOrgCode());
        eVo.setE_bizSerno(distrCaseApply);
        //流程标识
        eVo.setFlowId(flowId);
        //业务事件标识
        eVo.setEventId(eventId);
        eVo.setG_bizSerno(distrCaseApply);
        eVo.setBizLagelOrgCode(vo.getLoginUserOrgCode());
        eVo.setData(ctdAppVO);

        //判断流程配置岗位是否有效
        if (!flowCommonService.dutyFlowCheck(eventId, flowId)){
            throw new Exception("发起外催任务分案申请流程失败!原因: 流程岗位已失效");
        }

        HttpClientUtil.callFlowEngine(flowUrlConfigService.getRestfulSyncStartEventUrl(), eVo, String.class);
        logger.info("发起外催任务分案申请流程结束，分案申请流水号为:" + distrCaseApply);

        int num = colltTaskDistrAppService.insertColltTaskDistrApp(ctdAppVO);
        if (num != 1) {
            throw new Exception("外催任务分案申请信息插入失败！");
        }
    }

    public String getDistrCaseApply() throws Exception {
        String result = null;
        try {
            result = sequenceService.getSequence("DIV_CASE_SERNO", null, null);
        } catch (Exception e) {
            throw new ExcelException("生成分案申请流水号出错，错误信息为：" + e.getMessage());
        }
        return result;
    }

    public String getBatchNo() throws Exception {
        String result = null;
        try {
            result = sequenceService.getSequence("DIVIDE_BATCH", null, null);
        } catch (Exception e) {
            throw new ExcelException("生成分配批次号出错，错误信息为：" + e.getMessage());
        }
        return result;
    }

}
